require("gq-core/lib/global");
var co = require("gq-core/node_modules/co");
var init_obj = require("./biz/socket_init.js")
var socket = require("./lib/socket_connect.js");

function RouterInstaller(routers) {
    var prefix = routers.prefix || "";
    Object.keys(routers).forEach(method => {
        if (["get", "post", "put", "delete"].indexOf(method.toLowerCase()) === -1) {
            return;
        }
        var router = routers[method];
        Object.keys(router).forEach(path => {
            var register_info = {
                path: prefix + path,
                method: method
            };
            var config = router[path];
            if (config.length >= 2) {
                register_info = register_info.$mix(config[0]);
                var router_handle = config[1];
            } else {
                var router_handle = config[0];
            };
            router_handle = co.wrap(router_handle);
            socket.handles.routerRegister(register_info, router_handle);
        });
    });
};
co(function*() {
    yield init_obj.init;
    console.flag("开始注册路由");
    fs.lsAll(__dirname + "/fun").filter(file_path => file_path.endWith(".r.js"))
        .map(require).forEach(RouterInstaller);
}).catch(e => console.error);
